package com.box.app.arithmetic

interface Callable {
    val name: String

    val priority: Int get() = Int.MAX_VALUE

    @Throws(ArithmeticError::class)
    fun call(vararg values: Number): Number
}

internal fun Iterable<Callable>.maybeOf(s: String): Math? {
    for (element in this) {
        if (element.name == s || element.name == "`$s`") {
            return Math(object : Callable {
                override val name: String = s
                override val priority: Int = element.priority

                @Throws(ArithmeticError::class)
                override fun call(vararg values: Number) = element.call(*values)
            })
        }
    }
    return null
}

internal fun Iterable<Callable>.names() = this.map(Callable::name)